
/*:
* @plugindesc 楓之舞界面 - 滑鼠響應按鈕
* @author 老饕
* @version 1.0.0
* @date 2020.1.31

* @help
* 此插件實現圖片按鈕跟隨滑鼠響應。
* 需要放在CP_MouseMove後。
*/



//-----------------------------------------------------------------------------
//SWDA_Sprite_Button
//-----------------------------------------------------------------------------
// Sprite_Button
//
// The sprite for displaying a button.

var SWDA_Sprite_Button = SWDA_Sprite_Button || {};

//SWDA
SWDA_Sprite_Button.spriteButtonUpdate = Sprite_Button.prototype.update;
Sprite_Button.prototype.update = function() {
	SWDA_Sprite_Button.spriteButtonUpdate.call(this);
    this.processMouseMove();
};


//SWDA
Sprite_Button.prototype.processMouseMove = function(){
    if(this.isActive() && this.isMovedInsideFrame() && (TouchInput.movingdate > Input.date)){
		if (this._onFrame){
			this.onFrame();
		} 
		
    }
};

//-----------------------------------------------------------------------------
// Sprite_Button
//
// The sprite for displaying a button.
// 補充定義：按下（通常window中鍵盤控制時刷新按鈕）
// hot：按下
// cold：未觸碰
// on：觸摸/游標選中
Sprite_Button.prototype.hotFrame = function() {
    frame = this._hotFrame;
    this.setFrame(frame.x, frame.y, frame.width, frame.height);
};

// 補充定義：On（滑鼠移動到上面時變亮）
Sprite_Button.prototype.onFrame = function() {
	if (!this._touching){
		frame = this._onFrame;
		this.setFrame(frame.x, frame.y, frame.width, frame.height);
	}
};

Sprite_Button.prototype.setOnFrame = function(x, y, width, height) {
    this._onFrame = new Rectangle(x, y, width, height);
};


Sprite_Button.prototype.isMovedInsideFrame = function() {
    var x = this.canvasToLocalX(TouchInput.realX);
    var y = this.canvasToLocalY(TouchInput.realY);
    return x >= 0 && y >= 0 && x < this.width && y < this.height;
	
};


Sprite_Button.prototype.updateFrame = function() {
    var frame;
	if (this._touching && this.isActive()) {
        frame = this._hotFrame;
    } else {
        frame = this._coldFrame;
    }
    if (frame) {
        this.setFrame(frame.x, frame.y, frame.width, frame.height);
    }
	if (this.isEnabled()===false){
		this.setColorTone([-50, -50, -50, 220]);
	}else{
		this.setColorTone([0, 0, 0, 0]);
	}
	
};

Sprite_Button.prototype.isActive = function() {
    var node = this;
    while (node) {
        if (!node.visible) {
            return false;
        }
        node = node.parent;
    }
	if (!this.parent.isOpenAndActive() ) {
		return false;
	}
    return true;
};

Sprite_Button.prototype.isEnabled = function() {
	return this.enabled;
};
//------------------------------
//         TouchInput
//------------------------------

Object.defineProperty(TouchInput, 'movingdate', {//swda
    get: function() {
        return this._movingdate;
    },
    configurable: true
});

SWDA_Sprite_Button.TOUCH_CLEAR = TouchInput.clear;
TouchInput.clear = function() {
    SWDA_Sprite_Button.TOUCH_CLEAR.call(this);
	this._movingdate = 0;	//記錄移動滑鼠的時間
};




SWDA_Sprite_Button.TOUCH_UPDATE_MOVING = TouchInput.updateMoving;
TouchInput.updateMoving = function(){
	SWDA_Sprite_Button.TOUCH_UPDATE_MOVING.call(this);
    this._moving = this._oldX !== this.realX || this._oldY !== this.realY;
	if (this._moving){ this._movingdate = Date.now();}	//記錄移動滑鼠的時間
	
};